package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.businessService.AnnualReportBusinessService;
import com.ktwlsoft.archivemanage.domain.business.businessService.NotifySetService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.AnnualReportRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.ArchiveUnitRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.ReportLogRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.ArchiveUnit;
import com.ktwlsoft.archivemanage.domain.dao.entity.NotifySet;
import com.ktwlsoft.archivemanage.domain.dao.entity.ReportDetail;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.annualReport.*;
import com.ktwlsoft.archivemanage.payload.notifySet.AnnualReportRemindSetRequest;
import com.ktwlsoft.archivemanage.service.security.UserPrincipal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 年度报送控制器类
 *
 * @author chenkun
 * @version 1.0
 * @date 2019-11-01
 */
@Api(value = "年度报送", description = "年度报送")
@RestController
@RequestMapping("/annualReport")
public class AnnualReportController {

    private AnnualReportRepositoryService annualReportRepositoryService;

    private AnnualReportBusinessService annualReportBusinessService;

    private ArchiveUnitRepositoryService archiveUnitRepositoryService;

    private final ReportLogRepositoryService reportLogRepositoryService;

    private final NotifySetService notifySetService;

    public AnnualReportController(AnnualReportRepositoryService annualReportRepositoryService,
                                  AnnualReportBusinessService annualReportBusinessService,
                                  ArchiveUnitRepositoryService archiveUnitRepositoryService, ReportLogRepositoryService reportLogRepositoryService, NotifySetService notifySetService) {
        this.annualReportRepositoryService = annualReportRepositoryService;
        this.annualReportBusinessService = annualReportBusinessService;
        this.archiveUnitRepositoryService = archiveUnitRepositoryService;
        this.reportLogRepositoryService = reportLogRepositoryService;
        this.notifySetService = notifySetService;
    }

    /**
     * 年度报送列表查询
     *
     * @param request 查询条件
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "年度报送列表查询", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "findPagedList")
    public ResponseEntity<?> findPagedList(AnnualReportQueryRequest request) {
        return ResponseEntity.ok(annualReportRepositoryService.findPagedList(request));
    }


    /**
     * 年度报送列表查询(小程序端)
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "年度报送列表查询(小程序端)", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @GetMapping(value = "/findList")
    @ResponseBody
    public ResponseEntity<?> findAnnualReportList() {
        Account account = UserPrincipal.getCurrentUser();
        ArchiveUnit archiveUnit = archiveUnitRepositoryService.getByAccountId(account.getId());
        Map<String, Object> data = new HashMap<>();
        data.put("unitId", archiveUnit.getId());
        data.put("field","report_time");
        data.put("order","DESC");
        List<ReportDetail> list = annualReportRepositoryService.findAnnualReportList(data);
        Result result = Result.body(list, ResultType.BUSINESS_SUCCESS);
        return ResponseEntity.ok(result);
    }

    /**
     * 年度报送详情查询(小程序端)
     *
     * @param businessId 业务单号
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "年度报送详情查询(小程序端)", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @GetMapping(value = "/findOne")
    @ResponseBody
    public ResponseEntity<?> findAnnualReportInfo(@ApiParam(name = "businessId", value = "业务单号") @RequestParam Long businessId) {
        Result result = annualReportBusinessService.findOne(businessId);
        return ResponseEntity.ok(result);
    }

    /**
     * 年度报送信息编辑(小程序端)
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "年度报送信息编辑(小程序端)", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @PostMapping(value = "/updateOne")
    @ResponseBody
    public ResponseEntity<?> updateAnnualReport(@RequestBody AnnualReportUpdateRequest request,
                                                BindingResult bindingResult) {
        Result result = null;
        if (bindingResult.hasErrors()) {
            result = Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result = annualReportBusinessService.updateAnnualReport(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 年度报送新增(小程序端)
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "年度报送新增(小程序端)", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @PostMapping(value = "/addOne")
    @ResponseBody
    public ResponseEntity<?> addAnnualReport(@RequestBody AnnualReportAddRequest request,
                                             BindingResult bindingResult) {
        Result result = null;
        if (bindingResult.hasErrors()) {
            result = Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result = annualReportBusinessService.addAnnualReport(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 年度报送详情查询
     *
     * @param businessId 业务单号
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "年度报送详情查询", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @GetMapping(value = "/verifyPrepare")
    @ResponseBody
    public ResponseEntity<?> verifyPrepare(@ApiParam(name = "businessId", value = "业务编号") @RequestParam Long businessId) {
        Result result = annualReportBusinessService.findOne(businessId);
        return ResponseEntity.ok(result);
    }

    /**
     * 年度报送数据验证
     *
     * @param request 年度报送验证数据
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "年度报送数据验证", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @PostMapping(value = "/verifyConfirm")
    @ResponseBody
    public ResponseEntity<?> verifyConfirm(@RequestBody AnnualReportVerifyRequest request,
                                           BindingResult bindingResult) {
        Result result = null;
        if (bindingResult.hasErrors()) {
            result = Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result = annualReportBusinessService.verifyConfirm(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 注册字符串转成日期的编辑器
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @ApiOperation("年度报送操作日志")
    @GetMapping("/logs")
    public ResponseEntity<?> logs(@ApiParam("报送id") @RequestParam Long reportId) {
        return ResponseEntity.ok(Result.ok(reportLogRepositoryService.getReportLogMapper().selectAll(reportId)));
    }

    @ApiOperation("获取设置")
    @GetMapping("/getSet")
    public ResponseEntity<?> getSet(){
        return  ResponseEntity.ok(Result.ok(notifySetService.getNotifySet("REPORT_ANNUAL")));
    }

    @ApiOperation("设置")
    @PostMapping("/set")
    public ResponseEntity<?> setNotify(@RequestBody @Valid AnnualReportRemindSetRequest request){
        notifySetService.setAnnualReportNotifySet(request);
        return ResponseEntity.ok(Result.ok("设置成功！"));
    }
}
